<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//报告单管理
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'报告列表'),
	array('op'=>'edit','title'=>'添加报告'),
	array('op'=>'url','title'=>'查询入口'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if($_RQ['name']){
		$return['params']['name']=$_RQ['name'];
		$params['name like']=trim($_RQ['name']);
	}
	if($_RQ['mobile']){
		$return['params']['mobile']=$_RQ['mobile'];
		$params['mobile']=trim($_RQ['mobile']);
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$psize=intval($_RQ['psize'])?:15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>array('id','name','mobile','createtime'),
		'orderby'=>'id desc',
	);
	$list=pdo_getlist('core_report',$parameter,$total);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['psize'] =$psize;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','psize'=>$psize,'total'=>$total,'delall'=>false,'list'=>array(
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'createtime','title'=>'上传时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'string','name'=>'name','title'=>'姓名','width'=>'15'),
			array('type'=>'string','name'=>'mobile','title'=>'手机号','width'=>'15'),
			array('type'=>'time_range','name'=>'createtime','title'=>'上传时间','width'=>'40'),
		),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'name'=>trim($post['name']),
			'mobile'=>trim($post['mobile']),
			'reports'=>$post['reports']?ijson_encode($post['reports']):'',
			'remark'=>trim($post['remark']),
		);
		
		$id=intval($post['id']);
		if($id){
			pdo_update('core_report',$data,array('id'=>$id));
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'createtime'=>TIMESTAMP,
			);
			$data=array_merge($insert,$data);
			pdo_insert('core_report',$data);
		}
		exi('操作成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_report',array('id'=>$id));
		$item['reports']=$item['reports']?json_decode($item['reports'],true):array();
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'name','title'=>'姓名'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('type'=>'array_simple','name'=>'reports','title'=>'报告单','fields'=>array(
				array('type'=>'string','name'=>'title','title'=>'标题'),
				array('name'=>'image','title'=>'报告单图片','type'=>'image'),
			)),
			array('name'=>'remark','title'=>'医生建议','type'=>'text'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要删除的报告单！','error');
	}
	pdo_delete('core_report',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！');
}
if($_RQ['op']=='url'){
	$item=array('url'=>custom_url('jy_report/index/',true));
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'url','title'=>'查询地址','read'=>true),
			array('type'=>'ewm','name'=>'url','title'=>'入口二维码'),
		),
	);
	exi($return);
}